Yatzy = {
	
	intervalId : null,
	
	init:function()
	{
		var throwBtn = document.getElementById("rollDice");
		if (throwBtn !== null) {
			throwBtn.onclick = Yatzy.throwDice;
		}
		
		var saveBtn = document.getElementById("save");
		if (saveBtn !== null) {
			saveBtn.onclick = Yatzy.saveScore;	
		}
		
		//gör hela tärningsdiven klickbar
		for(var i = 1; i <= 5; ++i)
		{
			var dieDivCheckbox = document.getElementById("saveDie" + i);
			if(dieDivCheckbox.disabled == false)
			{
				dieDivCheckbox.parentNode.onclick = Yatzy.checkSaveDie;
			}
		}
		
		//gör hela tabellraden klickbar
		var resTable = document.getElementById('resTable');
		var tablerows = resTable.getElementsByTagName('tr');
		
		
		for(var i = 0; i< tablerows.length; ++i)
		{
			if(tablerows[i].getElementsByTagName('input').length === 1)
			{
				tablerows[i].onclick = Yatzy.checkSaveResult;
			}
		}
		
		//döljer checkboxarna
		var inputArr = document.getElementsByTagName('input');
		
		for(var i = 0; i<inputArr.length; ++i)
		{
			if(inputArr[i].className == "dieCheckbox")
			{
				inputArr[i].className += ' hidden';
			}
		}
		
		Yatzy.setLabelAfterCheckbox();
		Yatzy.disabelSaveButtonIfNoResultSet();
	},
	
	throwDice:function()
	{
		document.getElementById('rollDice').disabled = true;
		var parameters = "";
		
		if (document.getElementById("saveDie1").checked == true)
		{
			parameters += "saveDie1=on";
		}
		if (document.getElementById("saveDie2").checked == true)
		{
			if (parameters != "")
			{
				parameters += "&";
			}
			parameters += "saveDie2=on";		
		}
		if (document.getElementById("saveDie3").checked == true)
		{
			if (parameters != "")
			{
				parameters += "&";
			}
			parameters += "saveDie3=on";		
		}
		if (document.getElementById("saveDie4").checked == true)
		{
			if (parameters != "")
			{
				parameters += "&";
			}
			parameters += "saveDie4=on";		
		}
		if (document.getElementById("saveDie5").checked == true)
		{
			if (parameters != "")
			{
				parameters += "&";
			}
			parameters += "saveDie5=on";		
		}
		parameters += "&ajax=true";
		
		Yatzy.intervalId = window.setInterval(Yatzy.rollDiceAnimation,200);

		ajaxConPost.readData("content", document.location.href, parameters)
		return false;
	},
	
	saveScore:function()
	{
		document.getElementById('save').disabled = true;
		
		document.getElementById('saveloaderDiv').innerHTML = '<img src="/pics/form/ajax-loader.gif" alt="loader" />';
		
		var parameters = "";
	
		var f = document.getElementsByTagName("form")[0];
		for(var i = 0; i < f.length; ++i)
		{
			if (f[i].type == "radio")
			{
				if (f[i].checked == true)
				{
					parameters = parameters + f[i].name + "=" + f[i].value;
				}
			}
		}
		parameters += "&save=Spara&ajax=true";
		
//		ajaxConPost.readData("content","http://localhost/game/ajax", parameters);
		ajaxConPost.readData("content",document.location.href, parameters);
		return false;		
	},
	
	
	checkSaveDie:function()
	{
		var myInput = this.getElementsByTagName('input')[0];
		if(myInput.checked == false)
		{
			myInput.checked = true;
		}
		else
		{
			myInput.checked = false;
		}
		
		Yatzy.setLabelAfterCheckbox();
		
	},
	
	checkSaveResult:function()
	{
		var myInput = this.getElementsByTagName('input')[0];
		if(myInput.checked == false)
		{
			myInput.checked = true;
		}
		Yatzy.disabelSaveButtonIfNoResultSet();
	},
	
	setLabelAfterCheckbox:function()
	{
		var inputArr = document.getElementsByTagName('label');
		for(var i = 0; i<inputArr.length; ++i)
		{
			if(inputArr[i].className == "formLabel" || inputArr[i].className == "formLabel hidden")
			{
				if(inputArr[i].parentNode.getElementsByTagName('input')[0].checked)
				{
					inputArr[i].className = 'formLabel';
				}
				else
				{
					inputArr[i].className = 'formLabel hidden';
				}
			}
		}
	},
	
	disabelSaveButtonIfNoResultSet:function()
	{
		var saveButton = document.getElementById('save');
		saveButton.disabled = true;
		
		var inputArr = document.getElementsByTagName('input');
		
		for(var i = 0; i<inputArr.length; ++i)
		{
			if(inputArr[i].type == "radio")
			{
				if(inputArr[i].checked == true)
				{
					saveButton.disabled = false;
				}
			}
		}
	},
	
	
	rollDiceAnimation:function()
	{		
		var dieDivs = document.getElementsByClassName("dieDiv");
		for (var i = 0; i <= 4; ++i) {
			if (document.getElementById('saveDie' + (i + 1)).checked == false) {
				var dieImg = dieDivs[i].firstChild.nextSibling;
				var imgSrc = "/pics/dice/" + Math.floor(Math.random() * 6 + 1) + ".gif"
				dieImg.setAttribute("src", imgSrc);
			}
		}
	}

}
window.onload = Yatzy.init;




